﻿using System;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using System.Collections.Generic;

public class TestUIBase : UIBase
{
    /*定义组件*/
    public Transform Tran_UIRoot;
    public Image Img_BG;
    public UIButton Btn_Click1;
    public UIButton Btn_Click2;

    protected override void OnInitialize(Transform uiRoot)
    {
        base.OnInitialize(uiRoot);
        /*绑定组件*/
        Tran_UIRoot = uiRoot;
        Img_BG = Tran_UIRoot.Find("BG").GetComponent<Image>();
        Btn_Click1 = Tran_UIRoot.Find("Click1").GetComponent<UIButton>();
        Btn_Click2 = Tran_UIRoot.Find("Click2").GetComponent<UIButton>();
    }

    public override EUICanvas GetUICanvas()
    {
        return EUICanvas.Bottom;
    }
}

public class TestUI : TestUIBase
{
    public new const string NAME = "TestUI";

    #region msg
    /**************************
     * 消息机制使用实例
     * eg:
     * 
     * ************************/

    private void EvntHandler1()
    {
        Debug.Log("EvntHandler1 ");
    }
    private void EvntHandler2(string str)
    {
        Debug.Log("EvntHandler2 " + str);
    }

    #endregion

    #region btn event
    /****************************
     * 按钮事件部分
     * 工具会自动生成代码
     * **************************/
    private void BindingBtnEvent()
    {
        Btn_Click1.onClick.AddListener(BtnEvnt_Click1);
        Btn_Click2.onClick.AddListener(BtnEvnt_Click2);
    }

    private void BtnEvnt_Click1()
    {
        Debug.Log("BtnEvnt_Click1");
        MessageCenter.SendMessage(MessageID.UI_TEST_1);
    }

    private void BtnEvnt_Click2()
    {
        Debug.Log("BtnEvnt_Click2");
        MessageCenter.SendMessage(MessageID.UI_TEST_2, "click2");
    }
    #endregion

    #region flow
    /****************************
     * 界面流程函数
     * 自行继承需要用的函数重写
     * 
     * OnInitialize 只在第一次实例化UI的时候会调用
     * OnShow       每次打开UI调用
     * OnClose      每次关闭UI会调用(先于关闭动画调用，有顺序变更需要自行继承父类CloseUI函数重写)
     * OnDestory    摧毁UI的时候调用
     * **************************/
    protected override void OnInitialize(Transform uiRoot)
    {
        base.OnInitialize(uiRoot);
        BindingBtnEvent();
        Debug.Log("TestUI OnInitialize");
    }

    protected override void OnShow(params object[] paras)
    {
        base.OnShow(paras);
        //Debug.Log("TestUI OnShow " + paras[0].ToString() + " " + paras[1].ToString());

        MessageCenter.AddListener(MessageID.UI_TEST_1, EvntHandler1);
        MessageCenter.AddListener<string>(MessageID.UI_TEST_2, EvntHandler2);
    }

    protected override void OnClose()
    {
        base.OnClose();
        Debug.Log("TestUI OnClose");

        MessageCenter.RemoveListener(MessageID.UI_TEST_1, EvntHandler1);
        MessageCenter.RemoveListener<string>(MessageID.UI_TEST_2, EvntHandler2);
    }

    protected override void OnDestory()
    {
        base.OnDestory();
        Debug.Log("TestUI OnDestory");
    }
    #endregion

    #region animation
    /****************************
     * 界面动画函数
     * 自行继承重写
     * 
     * 注：关闭动画中<必须>执行回调
     * **************************/
    protected override void DoShowAnimation()
    {
        base.DoShowAnimation();
        Btn_Click1.transform.DOLocalMoveY(-95, 1);
        Debug.Log("TestUI DoShowAnimation");
    }

    protected override void DoCloseAnimation(Action callback)
    {
        Debug.Log("TestUI DoCloseAnimation");
        //base.DoCloseAnimation(callback);
        Btn_Click1.transform.DOLocalMoveY(102, 1).OnComplete(() =>
        {
            if (callback != null)
            {
                Debug.Log("TestUI DoCloseAnimation Callback");
                callback();
            }
        });
    }
    #endregion
}
